home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 85 / CD Temático 40 Febrero 2004.iso / DOS / testdisk / src / analyse.c < prev    next >
Encoding:
C/C++ Source or Header  |  2004-01-09  |  6.5 KB  |  207 lines

  1. /*
  2.  
  3.     File: analyse.c
  4.  
  5.     Copyright (C) 1998-2004 Christophe GRENIER <grenier@cgsecurity.org>
  6.   
  7.     This software is free software; you can redistribute it and/or modify
  8.     it under the terms of the GNU General Public License as published by
  9.     the Free Software Foundation; either version 2 of the License, or
  10.     (at your option) any later version.
  11.   
  12.     This program is distributed in the hope that it will be useful,
  13.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.     GNU General Public License for more details.
  16.   
  17.     You should have received a copy of the GNU General Public License
  18.     along with this program; if not, write to the Free Software
  19.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  
  21.  */
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include "types.h"
  25. #include "common.h"
  26. #include "fnctdsk.h" /* get_LBA_part */
  27. #include "analyse.h"
  28. #include "bfs.h"
  29. #include "swap.h"
  30. #include "bsd.h"
  31. #include "fat.h"
  32. #include "ntfs.h"
  33. #include "ext2.h"
  34. #include "netware.h"
  35. #include "intrface.h"
  36. #include "rfs.h"
  37. #include "lvm.h"
  38. #include "md.h"
  39.  
  40. int search_NTFS_backup(t_param_disk *disk_car,t_diskext *partition, const int debug, const int dump_ind)
  41. {
  42.   unsigned char buffer[SECTOR_SIZE];
  43.   if(disk_car->read(disk_car,1, &buffer, partition->lba)!=0)
  44.     return -1;
  45.   /* NTFS recovery using backup sector */
  46.   if(recover_NTFS(disk_car,(struct ntfs_boot_sector*)&buffer,partition,debug,dump_ind,1)==0)
  47.   {
  48.     strncpy(partition->info,"NTFS found using backup sector!",sizeof(partition->info));
  49.     return 1;
  50.   }
  51.   return 0;
  52. }
  53.  
  54. int search_FAT_backup(t_param_disk *disk_car,t_diskext *partition, const int debug, const int dump_ind)
  55. {
  56.   unsigned char buffer[3*SECTOR_SIZE];
  57.   if(disk_car->read(disk_car,3, &buffer, partition->lba)!=0)
  58.     return -1;
  59.   /* FAT32 recovery using backup sector */
  60.   if(recover_FAT32(disk_car,(const struct fat_boot_sector*)&buffer,partition,debug,dump_ind,1)==0)
  61.   {
  62.     strncpy(partition->info,"FAT found using backup sector!",sizeof(partition->info));
  63.     return 1;
  64.   }
  65.   return 0;
  66. }
  67.  
  68. int search_type_0(t_param_disk *disk_car,t_diskext *partition, const int debug, const int dump_ind)
  69. {
  70.   unsigned char buffer[8*SECTOR_SIZE];
  71.   if(disk_car->read(disk_car,8, &buffer, partition->lba)!=0)
  72.     return -1;
  73.   if(recover_Linux_SWAP(disk_car,(const union swap_header *)&buffer,partition,debug,dump_ind)==0) return 1;
  74.   if(recover_LVM(disk_car,(const pv_disk_t*)&buffer,partition,debug,dump_ind)==0) return 1;
  75.   if(recover_FAT12(disk_car,(const struct fat_boot_sector*)&buffer,partition,debug,dump_ind)==0) return 1;
  76.   if(recover_FAT16(disk_car,(const struct fat_boot_sector*)&buffer,partition,debug,dump_ind)==0) return 1;
  77.   if(recover_FAT32(disk_car,(const struct fat_boot_sector*)&buffer,partition,debug,dump_ind,0)==0) return 1;
  78.   if(recover_HPFS(disk_car,(const struct fat_boot_sector*)&buffer,partition,debug,dump_ind)==0) return 1;
  79.   if(recover_OS2MB(disk_car,(const struct fat_boot_sector*)&buffer,partition,debug,dump_ind)==0) return 1;
  80.   if(recover_NTFS(disk_car,(const struct ntfs_boot_sector*)&buffer,partition,debug,dump_ind,0)==0) return 1;
  81.   if(recover_netware((const struct disk_netware *)&buffer,partition)==0) return 1;
  82.   return 0;
  83. }
  84.  
  85. int search_type_1(t_param_disk *disk_car,t_diskext *partition,const int debug, const int dump_ind)
  86. {
  87.   unsigned char buffer[8*SECTOR_SIZE];
  88.   if(debug>1)
  89.   {
  90.     ecrit_rapport("search_type_1 lba=%lu\n",partition->lba);
  91.   }
  92.   if(disk_car->read(disk_car,8, &buffer, partition->lba)!=0)
  93.     return -1;
  94.   if(recover_BSD(disk_car,(const struct disklabel *)&buffer[SECTOR_SIZE],partition,debug,dump_ind)==0) return 1;
  95.   if(recover_BeFS(disk_car,(const struct disk_super_block *)&buffer[SECTOR_SIZE],partition,debug,dump_ind)==0) return 1;
  96.   return 0;
  97. }
  98.  
  99. int search_type_2(t_param_disk *disk_car,t_diskext *partition,const int debug, const int dump_ind)
  100. {
  101.   unsigned char buffer[8*SECTOR_SIZE];
  102.   if(debug>1)
  103.   {
  104.     ecrit_rapport("search_type_2 lba=%ld\n",partition->lba);
  105.   }
  106.   if(partition->lba+8<disk_car->size)
  107.   {
  108.     if(disk_car->read(disk_car,2, &buffer[0x400], partition->lba+2)==0)
  109.     {
  110.       if(recover_EXT2(disk_car,(const struct ext2_super_block*)&buffer[0x400],partition,debug,dump_ind)==0)
  111.       {
  112.     return 1;
  113.       }
  114.     }
  115.   }
  116.   return 0;
  117. }
  118.  
  119. int search_type_128(t_param_disk *disk_car,t_diskext *partition,const int debug, const int dump_ind)
  120. {
  121.   if(partition->lba+128<disk_car->size)
  122.   {
  123.     unsigned char buffer[8*SECTOR_SIZE];
  124.     if(debug>1)
  125.     {
  126.       ecrit_rapport("search_type_128 lba=%ld\n",partition->lba);
  127.     }
  128.     /* Test ReiserFS */
  129.     if(disk_car->read(disk_car,8, &buffer, partition->lba+128)!=0) /* 64k offset */
  130.       return -1;
  131.     if(recover_rfs(disk_car,(const struct reiserfs_super_block*)&buffer,partition,debug,dump_ind)==0) return 1;
  132.   }
  133.   return 0;
  134. }
  135.  
  136. int check_part(t_param_disk *disk_car,const int debug,t_diskext *partition)  
  137. {
  138.   int ret=0;
  139.   switch(partition->part_type)
  140.   {
  141.     case P_12FAT:
  142.     case P_16FAT:
  143.     case P_16FATBD:
  144.     case P_32FAT:
  145.     case P_32FAT_LBA:
  146.     case P_16FATBD_LBA:
  147.     case P_12FATH:
  148.     case P_16FATH:
  149.     case P_16FATBDH:
  150.     case P_32FATH:
  151.     case P_32FAT_LBAH:
  152.     case P_16FATBD_LBAH:
  153.       ret=check_FAT(disk_car,partition,debug);
  154.       break;
  155.     case P_LINUX:
  156.       ret=check_EXT2(disk_car,partition,debug);
  157.       if(ret!=0)
  158.     ret=check_rfs(disk_car,partition,debug);
  159.       if(ret!=0)
  160.       { aff_buffer(BUFFER_ADD,"No EXT2 or Reiser marker\n"); }
  161.       break;
  162.     case P_NTFS:
  163.     case P_NTFSH:
  164.       ret=check_NTFS(disk_car,partition,debug,0);
  165.       break;
  166.     case P_LVM:
  167.       ret=check_LVM(disk_car,partition,debug);
  168.       break;
  169.     case P_BEOS:
  170.       ret=check_BeFS(disk_car,partition,debug);
  171.       break;
  172.     case P_FREEBSD:
  173.       ret=check_BSD(disk_car,partition,debug,BSD_MAXPARTITIONS);
  174.       break;
  175.     case P_OPENBSD:
  176.       ret=check_BSD(disk_car,partition,debug,OPENBSD_MAXPARTITIONS);
  177.       break;
  178.     case P_NETBSD:
  179.       ret=check_BSD(disk_car,partition,debug,BSD_MAXPARTITIONS);
  180.       break;
  181.     case P_LINSWAP:
  182.       ret=check_Linux_SWAP(disk_car,partition,debug);
  183.       break;
  184.     case P_RAID:
  185.       ret=check_MD(disk_car,partition,debug);
  186.       if(ret!=0)
  187.       { aff_buffer(BUFFER_ADD,"Invalid RAID superblock\n"); }
  188.       break;
  189.     case P_EXTENDED:
  190.     case P_EXTENDX:
  191.     case P_LINUXEXTENDX:
  192.     case P_NETWARE:
  193.       break;
  194.     default:
  195.       if(debug>0)
  196.     ecrit_rapport("check_part %u type %02X: no test\n",partition->order,partition->part_type);
  197.       break;
  198.   }
  199.   if(ret!=0)
  200.   {
  201.     ecrit_rapport("check_part %02X\n", partition->part_type);
  202.     aff_part_buffer(AFF_PART_ORDER,disk_car,partition);
  203.   }
  204.   return ret;
  205. }
  206.  
  207.